#!/usr/bin/python
# -*- coding: UTF-8 -*-
# !@Time  : 2021/11/7 22:21
# !@Author: tongshu


import time
from pymysql_lib import UsingMysql

# 配置文件路径
srcFile = "rule.txt"

# SQL 查询语句
sql_select = "select count(0) as counts from (select phone from app_user where id >= (select id from app_user limit {start}, 1) limit 100000) a where phone like '{rule}'"
sql_counts = "select count(0) as counts from app_user"
# 数据表记录数
counts = 0
defaultLimit = 100000
limitCount = 0
remainderLimit = 0
# 欲查询记录数
phoneCounts = 0


def selectCount():
    start_time = time.perf_counter()
    global counts
    global remainderLimit
    global limitCount
    global phoneCounts
    with UsingMysql(log_time=False) as um:
        counts = um.get_count(sql_counts, None, 'counts')
        limitCount = counts // defaultLimit
        remainderLimit = counts % defaultLimit
        if remainderLimit != 0:
            limitCount += 1
    num = 0
    with open(srcFile, 'r') as f:
        for line in f.readlines():
            # print(line.strip())
            for i in range(limitCount):
                start = i * defaultLimit
                sql = sql_select.format(rule=line.strip(), start=start)
                print(sql)
                with UsingMysql(log_time=False) as um:
                    result = um.get_count(sql, None, 'counts')
                    num += result
    end_time = time.perf_counter()
    print(num)
    print('Running time: %s Seconds' % (end_time - start_time))


def test():
    start_time = time.perf_counter()
    sql = "select count(0) as phone_count from app_user where phone like '{rule}'"
    num = 0
    with open(srcFile, 'r') as f:
        for line in f.readlines():
            line.replace(" ")
            sql_each = sql.format(rule=line.strip())
            print(sql_each)
            with UsingMysql(log_time=False) as um:
                result = um.get_count(sql_each, None, 'phone_count')
                num += result
    end_time = time.perf_counter()
    print(num)
    print('Running time: %s Seconds' % (end_time - start_time))


def main():
    # selectCount()
    test()


if __name__ == '__main__':
    # electCount()
    test()
